package com.icard.wx.icardwx.controller;


import com.dsbj.common.model.District;
import com.dsbj.common.model.Page;
import com.dsbj.common.model.ReservationPrimary;
import com.dsbj.common.msg.ResponseMessage;
import com.icard.wx.icardwx.model.WxUser;
import com.icard.wx.icardwx.service.IReservationService;
import com.icard.wx.icardwx.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/reservation")
public class ReservationController extends BaseController{

    @Autowired
    private IReservationService reservationService;

    @RequestMapping("/list")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return "no_right";
        }
        Page<ReservationPrimary> pageData = new Page<>();
        ResponseMessage<Page<ReservationPrimary>> rsp =  reservationService.listByCreatorEmployeeNo(user.getEmployeeNo(),1,10);

        if(rsp!=null&&rsp.getData()!=null){
            pageData = rsp.getData();
        }

        model.addAttribute("pageData",pageData);

        return "reservation/page";
    }

    @RequestMapping("/getPageData")
    @ResponseBody
    public ResponseMessage pageData(Model model,Integer pageNo,Integer pageSize, HttpServletRequest request, HttpServletResponse response){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return ResponseMessage.error("没有权限");
        }
        Page<ReservationPrimary> pageData = new Page<>();
        ResponseMessage<Page<ReservationPrimary>> rsp =  reservationService.listByCreatorEmployeeNo(user.getEmployeeNo(),pageNo,pageSize);

        if(rsp!=null&&rsp.getData()!=null){
            pageData = rsp.getData();
        }

        return ResponseMessage.ok(pageData);
    }

    @RequestMapping(method = RequestMethod.POST,value = "/saveNew")
    @ResponseBody
    public ResponseMessage saveNew(Model model, HttpServletRequest request, HttpServletResponse response, ReservationPrimary reservationPrimary){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return ResponseMessage.error("没有权限提交数据");
        }
        System.out.println("开始提交");

        return reservationService.save(user,reservationPrimary);
    }


    @RequestMapping("/add")
    public String add(Model model, HttpServletRequest request, HttpServletResponse response){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return "no_right";
        }

        return "reservation/add";
    }

    @RequestMapping("/detail")
    public String detail(Model model, HttpServletRequest request, HttpServletResponse response,String id){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return "no_right";
        }

        ResponseMessage<ReservationPrimary> rsp = reservationService.getReservationPrimaryById(id);

        if(rsp!=null&&rsp.getData()!=null){
            model.addAttribute("reservationPrimary",rsp.getData());
        }else{
            model.addAttribute("reservationPrimary",new ReservationPrimary());
        }

        return "reservation/detail";
    }


    @RequestMapping("/getNewReservationNo")
    @ResponseBody
    public ResponseMessage getNewReservationNo(Model model, HttpServletRequest request, HttpServletResponse response){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return ResponseMessage.error("没有权限");
        }

        return reservationService.getReservationNo();
    }

    @RequestMapping("/getDistricts")
    @ResponseBody
    public ResponseMessage getDistricts(HttpServletRequest request){

        WxUser user = (WxUser) request.getSession().getAttribute(Constants.SESSION_USER);
        if(user==null){
            return ResponseMessage.error("没有权限");
        }

        List<District> dataList = new ArrayList<>();
        ResponseMessage<List<District>> rsp =  reservationService.getDistricts();

        if(rsp!=null&&!CollectionUtils.isEmpty(rsp.getData())){
            dataList = rsp.getData();
        }
        return ResponseMessage.ok(dataList);
    }
}
